package com.tangpian.sna.core.analysis.svm.pre;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.List;

import edu.udo.cs.wvtool.config.WVTConfiguration;
import edu.udo.cs.wvtool.config.WVTConfigurationFact;
import edu.udo.cs.wvtool.generic.loader.SourceAsTextLoader;
import edu.udo.cs.wvtool.generic.stemmer.DummyStemmer;
import edu.udo.cs.wvtool.generic.vectorcreation.TFIDF;
import edu.udo.cs.wvtool.generic.wordfilter.DummyWordFilter;
import edu.udo.cs.wvtool.main.WVTDocumentInfo;
import edu.udo.cs.wvtool.main.WVTFileInputList;
import edu.udo.cs.wvtool.main.WVTool;
import edu.udo.cs.wvtool.util.WVToolException;
import edu.udo.cs.wvtool.wordlist.WVTWordList;

public class TextVectorBuilder {

	private WVTConfiguration configuration;

	public TextVectorBuilder() {
		DummyStemmer stemmer = new DummyStemmer();

		DummyWordFilter wordFilter = new DummyWordFilter();
		wordFilter.setMinNumChars(2);

		VectorTokenizer tokenizer = new VectorTokenizer();

		configuration = new WVTConfiguration();

		configuration.setConfigurationRule(WVTConfiguration.STEP_STEMMER,
				new WVTConfigurationFact(stemmer));
		configuration.setConfigurationRule(WVTConfiguration.STEP_WORDFILTER,
				new WVTConfigurationFact(wordFilter));
		configuration.setConfigurationRule(WVTConfiguration.STEP_TOKENIZER,
				new WVTConfigurationFact(tokenizer));
		configuration.setConfigurationRule(
				WVTConfiguration.STEP_VECTOR_CREATION,
				new WVTConfigurationFact(new TFIDF()));
		configuration.setConfigurationRule(WVTConfiguration.STEP_LOADER,
				new WVTConfigurationFact(new SourceAsTextLoader()));

	}

	public void createTrainVectorFile(List<String>[] datas, String output) {
		try {
			WVTool tool = new WVTool(false);
			WVTFileInputList inputList = new WVTFileInputList(datas.length);

			for (int i = 0; i < datas.length; i++) {
				StringBuffer buffer = new StringBuffer();
				for (String data : datas[i]) {
					buffer.append(data);
				}
				inputList.addEntry(new WVTDocumentInfo(buffer.toString(), "", "", "", i));
			}

			// Generate the word list
			/*
			 * 生成wordList
			 */
			WVTWordList wordList = tool
					.createWordList(inputList, configuration);

			// Prune the word list
			/*
			 * 对wordList中DocumentFrequency做出一个限制，即DocumentFrequency在1<n<5之间
			 * 以前的解释是termOccurs，这个解释是错误的。有兴趣的看看源码就知道了。 （今天屈伟问起这个问题我才发现这个错误）。
			 */
			wordList.pruneByFrequency(1, 5);

			// /*
			// * 生成词组文件
			// */
			// wordList.storePlain(new FileWriter("wordlist.txt"));
			//
			// /*
			// * 生成词频文件 （wordList）
			// */
			// wordList.store(new FileWriter("wordVector.txt"));
			//
			// // Create the word vectors
			//
			// // Set up an output filter (write sparse vectors to a file)
			// /*
			// * 将生成的文本向量空间写入一个特定的文件
			// */
			// FileWriter outFile = new FileWriter("wv.txt");
			// // DummyWordVectorWriter wvw = new DummyWordVectorWriter(outFile,
			// // true);
			Writer writer = new BufferedWriter(new FileWriter(output));
			WordVectorWriter wvw = new WordVectorWriter(writer);

			configuration.setConfigurationRule(WVTConfiguration.STEP_OUTPUT,
					new WVTConfigurationFact(wvw));

			// Create the vectors
			tool.createVectors(inputList, configuration, wordList);

			// Close the output file
			// wvw.close();
			// outFile.close();

			// Just for demonstration: Create a vector from a String
			/*
			 * 一个使用wordList构建文本空间向量的实例
			 */
			// WVTWordVector q = tool.createVector("cmu harvard net", wordList);
			wvw.close();
			writer.close();
		} catch (WVToolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void createTestVectorFile(List<String> datas, String output) {
		try {
			WVTool tool = new WVTool(false);
			WVTFileInputList inputList = new WVTFileInputList(1);

			for (String data : datas) {
				inputList.addEntry(new WVTDocumentInfo(data, "", "", ""));
			}

			// Generate the word list
			/*
			 * 生成wordList
			 */
			WVTWordList wordList = tool
					.createWordList(inputList, configuration);

			// Prune the word list
			/*
			 * 对wordList中DocumentFrequency做出一个限制，即DocumentFrequency在1<n<5之间
			 * 以前的解释是termOccurs，这个解释是错误的。有兴趣的看看源码就知道了。 （今天屈伟问起这个问题我才发现这个错误）。
			 */
			wordList.pruneByFrequency(1, 5);

			// /*
			// * 生成词组文件
			// */
			// wordList.storePlain(new FileWriter("wordlist.txt"));
			//
			// /*
			// * 生成词频文件 （wordList）
			// */
			// wordList.store(new FileWriter("wordVector.txt"));
			//
			// // Create the word vectors
			//
			// // Set up an output filter (write sparse vectors to a file)
			// /*
			// * 将生成的文本向量空间写入一个特定的文件
			// */
			// FileWriter outFile = new FileWriter("wv.txt");
			// // DummyWordVectorWriter wvw = new DummyWordVectorWriter(outFile,
			// // true);

			Writer writer = new BufferedWriter(new FileWriter(output));
			WordVectorWriter wvw = new WordVectorWriter(writer);

			configuration.setConfigurationRule(WVTConfiguration.STEP_OUTPUT,
					new WVTConfigurationFact(wvw));

			// Create the vectors
			tool.createVectors(inputList, configuration, wordList);

			// Close the output file
			// wvw.close();
			// outFile.close();

			// Just for demonstration: Create a vector from a String
			/*
			 * 一个使用wordList构建文本空间向量的实例
			 */
			// WVTWordVector q = tool.createVector("cmu harvard net", wordList);
			wvw.close();
			writer.close();
		} catch (WVToolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
